//把对象调整到中心位置
;(function($){
    $.fn.setmiddle = function() {
        var dl = $(document).scrollLeft(),
            dt = $(document).scrollTop(),
            ww = $(window).width(),
            wh = $(window).height(),
            ow = $(this).width(),
            oh = $(this).height(),
            left = (ww - ow) / 2 + dl,
            top = (wh - oh) / 2 + dt;
        $(this).css({left:Math.max(left, dl) + "px",top:Math.max(top, dt) + "px"}); 
        return this;
    }
})(jQuery);

//提示信息
;(function($){
    $.tips = function(options) {
        var settings = {
            content: "",
            icon: "success",
            time: 1500,
            close: false,
            zindex: 2999
        };
        if (options) {
            $.extend(settings, options);
        }
        if (settings.close) {
            $(".tips").hide();
            return;
        }
        if (!$(".tips")[0]) {
            $("body").append('<div class="tips"><i></i><span></span></div>');
            $(".tips").css("z-index", parseInt(settings.zindex));
        }
        $(".tips span").html(settings.content);
        $(".tips").attr("class", "tips tips-" + settings.icon);
        $(".tips").css("z-index", parseInt($(".tips").css("z-index"))+1).setmiddle().show();
         
        if (settings.time > 0) {
            setTimeout(function() {
                $(".tips").fadeOut()
            }, settings.time);
        }
    }
})(jQuery);

//设置cookie
var setCookie = function(cname, cvalue, exdays) {
    var day = new Date();
    day.setTime(day.getTime() + (exdays*24*60*60*1000));
    document.cookie = cname + "=" + encodeURI(cvalue) + "; " + "expires=" + day.toUTCString() +"; path=/";
};

//获取cookie
var getCookie = function(cname) {
    var name = cname + "=";
    var obj = document.cookie.split(';');
    for (var i = 0; i < obj.length; i++) {
        var c = obj[i];
        while (c.charAt(0)==' ') c = c.substring(1);
        if (c.indexOf(name) != -1) return c.substring(name.length, c.length);
    }
    return "";
};

//清除cookie 
var clearCookie = function(cname) { 
    setCookie(cname, "", -1); 
};

//检查cookie 
var checkCookie = function(cname) {
    var value = getCookie(cname);
    if (value != "") {
        return true;
    }
    else {
        return false;
    }
};

$(function(){
    //首页
    $(".icon-home").bind("click",function(){
        window.location.href="/";
    });
    //回到顶部
    $(".totop").bind("click",function(){
        $('body,html').animate({
            scrollTop: 0
        }, 1000);
        return false;
    });
    //搜索
    $(".icon-search").bind("click",function(event){ 
        $(".menu").removeClass("show");
        $(".search").toggleClass("show")
        event.stopPropagation();
        $(document).bind("click",function(event){
            if (!$('.search-panel').is(event.target) && $('.search-panel').has(event.target).length === 0) {
                $(".search").removeClass("show");
            }
        });
    });
    //菜单
    $(".icon-menu").bind("click",function(){
        $(".search").removeClass("show");
        $(".menu").toggleClass("show")
        event.stopPropagation();
        $(document).bind("click",function(event){
            if (!$('.menu-panel').is(event.target) && $('.menu-panel').has(event.target).length === 0) {
                $(".menu").removeClass("show");
            }
        });
    });
    //书架
    var loop = "";
    var json = [
        {
            "bookid": "123456",
            "pic": "/static/images/p/b1.jpg",
            "title": "闪婚老公太凶猛",
            "chapterid": "111111",
            "chaptername": "第14章 他做了见不得人的事",
            "status": "serial"
        },
        {
            "bookid": "123456",
            "pic": "/static/images/p/b2.jpg",
            "title": "法医老婆不太冷",
            "chapterid": "111111",
            "chaptername": "第316章 亲爱的，你可算来了",
            "status": "finish"
        }
    ];
    for (var i = 0; i <= json.length - 1; i++) {
        var status = "";
        if (json[i].status == "serial") {
            status = "连载中";
        }
        else {
            status = "已完本";
        }
        loop += "<li>";
        loop += "<img class=\"pic\" src=\""+json[i].pic+"\" alt=\""+json[i].title+"\">";
        loop += "<a class=\"tit\" href=\"#\">"+json[i].title+"</a>";
        loop += "<p class=\"update\">更新至：<br>"+json[i].chaptername+"</p>";
        //loop += "<p class=\"read\"><a href=\"#\">继续阅读</a></p>";
        loop += "<i class=\"del\">删除</i>";
        loop += "</li>"
    }
    var html = "<div class=\"bookshelf-mask\"></div>";
        html += "<div class=\"bookshelf-panel\">";
        html += "<div class=\"bookshelf-head\"><h4>书架</h4><a class=\"bookshelf-set\" target=\"_self\">设置</a></div>";
        html += "<div class=\"bookshelf-list\">";
        html += "<ul>";
        //html += loop;
        html += "</ul>";
        html += "</div>"
        html += "</div>"
    if (($(".bookshelf-mask").length == 0) && $(".bookshelf-panel").length == 0) {
        $("body").append(html);
    }
    $(".icon-bookshelf,.btn-bookshelf").bind("click",function(){
        if (($(".bookshelf-mask").hasClass("show")) && $(".bookshelf-panel").hasClass("show")) {
            $(".bookshelf-panel").removeClass("show");
            $(".bookshelf-mask").removeClass("show");
        }
        else {
            $(".bookshelf-panel").addClass("show");
            $(".bookshelf-mask").addClass("show");
        }
    });
    $(".bookshelf-mask").bind("click",function(){
        $(".bookshelf-panel").removeClass("show");
        $(".bookshelf-mask").removeClass("show");
    });
    $(".bookshelf-set").live("click", function() {
        $(".bookshelf-head").toggleClass("edit");
        $(".bookshelf-list").toggleClass("edit");
    });
    $(".bookshelf-list li .del").live("click", function() {
        if(confirm("确定从书架中移除？")) {
            $(this).parent().slideUp(300, function(){
                $(this).remove();
            });
        }
    });
    //选项卡切换
    $(".mod-tabs ol li").bind("click",function(){
        var index = $(this).index();
        $(this).addClass("on").siblings("li").removeClass("on");
        $(".mod-body").eq(index).show().siblings(".mod-body").hide();
    });
    //专题页
    $(".tags-info section i").bind("click",function(){
        $(this).toggleClass("expand");
        $(".tags-info article").toggleClass("expand");
    });
    //资讯
    $(".sort .sort-asc").bind("click",function(){
        $(this).addClass("on");
        $(".sort .sort-desc").removeClass("on");
        $(".chapters ul").css("transform","rotate(0)");
        //$(".chapters li").css("display","block");
        $(".chapters li").css("transform","rotate(0)");
    });
    $(".sort .sort-desc").bind("click",function(){
        $(this).addClass("on");
        $(".sort .sort-asc").removeClass("on");
        $(".chapters ul").css("transform","rotate(180deg)");
        //$(".chapters li").css("display","block");
        $(".chapters li").css("transform","rotate(180deg)");
    });
    // var xx = function() {
    //     var chapters  =  $(".news-info .chapters ul").children("li").length;
    //     if (chapters >= 100) {
    //         $(".news-info .chapters ul").children("li").slice(100).css("display","none");
    //         $(".news-info .chapters").append("<p class='chapters-more'><a>显示更多</a></p>")
    //         $(".news-info .chapters-more").live("click",function(){
    //             $(this).css("display","none");
    //            $(".news-info .chapters ul").children("li").slice(100).css("display","block");
    //         });
    //     }
    // };
    //小说页
    $(".book-info .intro i").bind("click",function(){
        $(this).toggleClass("expand");
        $(".book-info .intro p").toggleClass("expand");
    });
    $(".go-review").bind("click",function(){
        $('body,html').animate({
            scrollTop: $(".mod-review").offset().top
        }, 300);
        return false;
    });
    $(".j-textarea").live("focus", function() {
        if ($(this).val() == "说点什么吧，您的评论是对本书最大的支持！") {
            $(this).val("")
        }
    }).live("blur", function() {
        if ($.trim($(this).val()) == "") {
            $(this).val("说点什么吧，您的评论是对本书最大的支持！")
        }
    }).live("keydown", function(){  
        var len = $(".j-textarea").val().length;
        if (len >= 200) {
            var num = $(".j-textarea").val().substr(0,200);;
            $(".j-textarea").val(num);;
        }
        else {
            $(".j-count").text(200-len-1);
        }
        $(".form-msg").text("");
    });
    $(".j-send").bind("click",function(){
        var txt = $(".j-textarea").val();
        var len = $(".j-textarea").val().length;
        if (txt == "说点什么吧，您的评论是对本书最大的支持！" || len == 0) {
            $(".form-msg").text("评论内容不能为空");
            return false;
        }
        else {
            alert("您的评论已提交，审核过后您的评论会出现在这里！");
        }
    });
    var $review = $(".review-list ul");
    if ($review.children("li").length > 5) {
        $review.children("li").slice(5).css("display","none");
        $(".review-more").bind("click",function(){
            $(this).css("display","none");
            $review.children("li").slice(5).css("display","block");
        });
    }
    //分页
    $(".pages dt").bind("click",function(){
        if ($('.pages dd').css("display") == "none") {
            $('.pages dl').addClass("on");
            $('.pages dd').slideDown(100);
        }
        else {
            $('.pages dl').removeClass("on");
            $('.pages dd').slideUp(100);
        }
        
    });
    //转到PC端
    // var url = window.location.href;
    // url = url.replace(/\/\/cp\.xiayiqu\.XXX/,"//www.hs13.XXX");
    // url = url.replace(/\?view=pc/,"") + "?view=pc";
    // $(".topc a").attr({"href":url, "target":"_blank"});
    //搜索输入字符串合法性验证
    $("#search-btn").click(function(){
        var str = $("#q").val();
        var ret = /[^\w\u4e00-\u9fa5]/;
        if(ret.test(str)){
           alert("搜索内容不合法");
           return false;
        }else{
           return ture;
        }
    });
});